package br.brpv.silvestrin.vendas.domain;

import java.util.ArrayList;

import br.brpv.silvestrin.vendas.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class EstablishmentAdapter extends ArrayAdapter<Establishment> {
    Context context;
    int layoutResourceId = R.layout.list_view_rows_establishments;
    ArrayList<Establishment> data = null;

    public EstablishmentAdapter(Context context, ArrayList<Establishment> data){
    	super(context, R.layout.list_view_rows_establishments, data);

        this.context = context;
        this.data = data;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        EstablishmentHolder holder = null;

        if(row == null) {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new EstablishmentHolder();
            holder.cod_estab = (TextView)row.findViewById(R.id.establishment_cod_estab);
            holder.nome_fantasia = (TextView)row.findViewById(R.id.establishment_nome_fantasia);
            holder.cidade = (TextView)row.findViewById(R.id.establishment_cidade);

            row.setTag(holder);
        } else {
            holder = (EstablishmentHolder)row.getTag();
        }

        Establishment estab = data.get(position);
        if(estab != null) {
            if(holder.cod_estab != null) holder.cod_estab.setText(estab.getCodEstablishment());
            if(holder.nome_fantasia != null) holder.nome_fantasia.setText(estab.getNomeFantasia());
            if(holder.cidade != null) holder.cidade.setText(estab.getCidade());
        }
        
        return row;
    }

    private static class EstablishmentHolder {
        TextView cod_estab;
        TextView nome_fantasia;
        TextView cidade;
    }
}
